require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')
require('../libs/iconfont/iconfont.css')
require('../css/courseTraining.less')

require('weui')
const weui = require('weui.js')

const axios = require('axios')


document.addEventListener('DOMContentLoaded', function () {
    let mask = document.querySelector('.mask')
    let stop = document.querySelector('.icon-stop')
    let goOn = document.querySelector('.goOn')
    let finishes = document.querySelector('.finishes')
    let videoUrl = document.querySelector("#videoUrl")
    let icon = document.querySelector("#icon")
    const serUrl = 'http://139.9.177.51:8099'

    //获取视频信息并渲染
    let fragments = JSON.parse(sessionStorage.fragments);
    console.log(fragments);
    let index = 0
    function renderData() {
        videoUrl.src = serUrl + fragments[index].videoUrl
        sectionNum.textContent = index + 1 + ' /'
        toSecNum.textContent = fragments.length
        curName.textContent = fragments[index].title
        imgUrl.src = serUrl + fragments[index].imgUrl
        document.querySelector(".projectName").textContent = fragments[index].title
    }
    renderData()
    videoUrl.autoplay = true
    // 点击暂停按钮显示模态框
    stop.addEventListener('click', function () {
        mask.style.display = 'block'
    })

    // 点击继续按钮隐藏模态框
    goOn.addEventListener('click', function () {
        mask.style.display = 'none'
        videoUrl.play();
        icon.className = 'iconfont icon-stop'
        recordTime()
    })

    //暂停
    icon.addEventListener('click', function () {
        videoUrl.pause();
        this.className = 'iconfont icon-bofang'
        clearInterval(timeId)
        console.log(taketime);
    })

    //进度条
    videoUrl.addEventListener('timeupdate', function () {
        let totalTime = videoUrl.duration
        let nowTime = videoUrl.currentTime
        // console.log(nowTime);
        document.querySelector("#program").style.width = nowTime / totalTime * 100 + '%'

    })
    //自动切换
    videoUrl.addEventListener('ended', function () {
        //当index===3可进行是否返回第一节
        console.log(index);
        if (index === fragments.length - 1) {
            clearInterval(timeId)
            weui.confirm('是否返回第一节', {
                buttons: [{
                    label: '否',
                    type: 'default',
                    onClick: function () {
                        document.querySelector("#finishes").click()
                    }
                }, {
                    label: '是',
                    type: 'primary',
                    onClick: function () {
                        index = 0
                        renderData()
                        recordTime()
                    }
                }]
            });
        } else {
            weui.toast('即将播放下一节', {
                callback: function () {
                    document.querySelector("#next").click()
                }
            }, 1500)
        }
    })



    //切换下一节
    document.querySelector("#next").addEventListener('click', function () {
        index++
        if (index === 4) {
            index = 0
        }
        renderData()
    })
    //切换上一节
    document.querySelector("#last").addEventListener('click', function () {
        index--
        if (index === -1) {
            index = 3
        }
        renderData()
    })

    //记录时间
    let taketime = 0
    function recordTime() {
        timeId = setInterval(function () {
            taketime++
        }, 1000)
    }
    recordTime()
    console.log(taketime);

    //点击结束按钮记录运动信息
    document.querySelector("#finishes").addEventListener('click', function () {
        let params = {
            id: sessionStorage.cid,
            userId: localStorage.userId,
            takeTime: taketime,
            calorie: taketime * 0.36
        }
        console.log(params);
        axios.post(serUrl + '/sports/saveTrain', params).then(function (res) {
            console.log(res);
            if (res.data.status === 0) {
                weui.toast("数据保存成功", {
                    callback: function () {
                        location.href = './sportsCourseTraining.html'
                    }
                })
            } else {
                weui.toast("继续努力")
            }
        })


    })
})